package com.example.myapplication.Activity;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.Utils.HttpUtils;
import com.example.myapplication.InfoBean;
import com.example.myapplication.adapter.ItemMain2Adapter;
import com.example.myapplication.R;
import com.example.myapplication.VideoBean;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class Main2Activity extends AppCompatActivity {

    private ListView main2Listview;
    String url = "http://baobab.kaiyanapp.com/api/v4/tabs/selected?udid=11111&vc=168&vn=3.3.1&deviceModel=Huawei6&first_channel=eyepetizer_baidu_market&last_channel=eyepetizer_baidu_market&system_version_code=20";
    List<VideoBean>videoBeanList;
    List<InfoBean.ItemListBean.DataBean.ProviderBean> list = new ArrayList<>();
    ItemMain2Adapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();
        load tak = new load();
        tak.execute(url);
        videoBeanList=new ArrayList<>();
        adapter=new ItemMain2Adapter(this,videoBeanList);
        main2Listview.setAdapter(adapter);
    }

    private void initView() {
        main2Listview = (ListView) findViewById(R.id.main2_listview);

    }

    class load extends AsyncTask<String, Void, String> {
        ProgressDialog dialog;

        public load() {
            dialog = new ProgressDialog(Main2Activity.this);
            dialog.setTitle("显示信息");
            dialog.setMessage("加载中");
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog.show();
            //开启子线程，执行异步请求之前，会调用的函数，通常执行初始化
        }

        @Override
        protected String doInBackground(String... strings) {
            //开启子线程，进行耗时操作。、，将获取到的数据，返回给主线程
            String json = HttpUtils.getJsonByNetwork(strings[0]);
            return json;
        }


        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            //回到主线程，将子线程传回来的数据，进行操作，更新UI
            //判断如果子线程返回的数据不为空，就显示在UI控件上

            if (!TextUtils.isEmpty(s)) {
                dialog.dismiss();
                Gson gson = new Gson();
                InfoBean infoBean = gson.fromJson(s, InfoBean.class);
                List<InfoBean.ItemListBean> itemListBeans = infoBean.getItemList();

                for (int i = 0; i < itemListBeans.size(); i++) {
                    InfoBean.ItemListBean itemBean = itemListBeans.get(i);
                    String type = itemBean.getType();

                    if (type.equals("video")) {
                        InfoBean.ItemListBean.DataBean dataBean = itemListBeans.get(i).getData();
                        InfoBean.ItemListBean.DataBean.ProviderBean provider = dataBean.getProvider();
                        InfoBean.ItemListBean.DataBean.ConsumptionBean consumption = dataBean.getConsumption();
                        VideoBean videoBean=new VideoBean(itemListBeans.get(i).getData().getTitle(),
                                itemListBeans.get(i).getData().getPlayUrl(),itemListBeans.get(i).getData().getProvider().getIcon(),
                                itemListBeans.get(i).getData().getProvider().getName(),consumption.getReplyCount()+"",consumption.getCollectionCount()+"" );
                        videoBeanList.add(videoBean);
                    }

                }
                adapter.notifyDataSetChanged();


            }
        }
    }

}
